<?
class CheckStatusForm extends Form
{
    function CheckStatusForm()
    {
        Form::Form('CheckStatusForm');

        $this->link_css('style/ManageCheckStatus/checkstatus.css');
        $this->link_css('style/jquery-ui-1.9.2.custom.css');

        $this->link_js('javascript/jquery/jquery-ui-1.9.2.custom.min.js');
        //
    }

    function draw()
    {
        global $display;

        $this->autoAddMessages('message');

        $display->add("WEB_ROOT",WEB_ROOT);

        $display->add("WEB_DIR",WEB_DIR);

        $hotel_id = url::getInt('hotel_id');

        $display->add("hotel_id", $hotel_id);
        
        $hotel = Db_Hotel::getHotelById($hotel_id);

        $type 			= Url::getInt('type',1);
        //$month_choice 	= Url::get("month_choice",date("m/Y",TIME_NOW));;

        $int_from_date 	= 0;
        $int_to_date 	= 0;


        if($type == 1)
        {
            $month_choice 	= Url::get("month_choice",date("m/Y",TIME_NOW));

            $get_from_date 	= explode("/", $month_choice);

            if(count($get_from_date) != 2 || !DateTimeUtil::valid_date(null,$get_from_date[0],$get_from_date[1]))
            {
                $month_choice 	= date("m/Y",TIME_NOW);
                //$int_from_date 	= explode("/", $month_choice);
            }

            $days_in_month  = cal_days_in_month(CAL_GREGORIAN, (int)$get_from_date[0], (int)$get_from_date[1]);

            $int_from_date 	= mktime("0","0","0", 	(int)$get_from_date[0],1, 					(int)$get_from_date[1]);
            $int_to_date 	= mktime("23","59","59",(int)$get_from_date[0],(int)$days_in_month,	(int)$get_from_date[1]);

            $display->add("title","Tình trạng phòng tháng {$get_from_date[0]}/{$get_from_date[1]}");

            $display->add("month_choice",$month_choice);
        }
        else
        {
            $from_date 		= Url::get("fromdate");
            $to_date 		= Url::get("todate");

            $get_from_date 	= explode("/",$from_date);

            if(count($get_from_date) != 2 || !DateTimeUtil::valid_date(null,$get_from_date[0],$get_from_date[1]))
            {
                $int_from_date = mktime("0","0","0",$get_from_date[1],$get_from_date[0],$get_from_date[2]);
            }

            $get_to_date 	= explode("/",$to_date);

            if(count($get_to_date) != 2 || !DateTimeUtil::valid_date(null,$get_to_date[0],$get_to_date[1]))
            {
                $int_to_date 	= mktime("23","59","59",$get_to_date[1],$get_to_date[0],$get_to_date[2]);
            }

            $display->add("fromdate",	$from_date);
            $display->add("todate",		$to_date);

            $display->add("title","Tình trạng phòng từ  ngày {$get_from_date[0]}/{$get_from_date[1]}/{$get_from_date[2]} đến {$get_to_date[0]}/{$get_to_date[1]}/{$get_to_date[2]}");
        }

        $display->add("type", $type);

        //Data for combobox select
        $cmbox_date = array(0 => "--Chọn tháng--");

        if($int_from_date)
        {
            $temp_month = date("m",$int_from_date)-7;
            $temp_year 	= date("Y",$int_from_date);
        }
        else
        {
            $temp_month = date("m",TIME_NOW)-7;
            $temp_year 	= date("Y",TIME_NOW);
        }

        if($temp_month<1) {
            $temp_month = 12 + $temp_month;
            $temp_year--;
        }

        for($i=0; $i<33; $i++)
        {
            $next_month = DateTimeUtil::get_next_month($temp_month,$temp_year);

            $temp_month = $next_month[0];
            $temp_year 	= $next_month[1];

            $cmbox_date["$temp_month/$temp_year"] = "$temp_month/$temp_year";
        }

        $display->add("cmbox_date",			 $cmbox_date);
        $display->add("cmbox_date_selected", $month_choice);

        $getNextMonth = DateTimeUtil::get_next_month(date("n",$int_from_date),date("Y",$int_from_date));
        $getPrevMonth = DateTimeUtil::get_prev_month(date("n",$int_from_date),date("Y",$int_from_date));

        $display->add("link_next",WEB_ROOT."check_room_status.html?hotel_id={$hotel['id']}&type=1&month_choice={$getNextMonth[0]}/{$getNextMonth[1]}");

        $display->add("link_prev",WEB_ROOT."check_room_status.html?hotel_id={$hotel['id']}&type=1&month_choice={$getPrevMonth[0]}/{$getPrevMonth[1]}");

        $display->add("link_now",WEB_ROOT."check_room_status.html?hotel_id={$hotel['id']}&type=1&month_choice=" . date("m/Y", TIME_NOW));

        $days 		= array();
        $daysInt 	= array();

        $now_day 	= mktime(0,0,0, date('m',TIME_NOW), date('d',TIME_NOW), date('Y',TIME_NOW));
        $time 		= $int_from_date;

        while($time < $int_to_date)
        {
            $d = date("j", $time);
            $w = date("D", $time);
            $m = date("n", $time);

            if(in_array($w, array("Sat", "Sun")))
            {
                $day 	= "<span color=red>$w $d/$m</san>";
            }
            else
            {
                $day 	= "$w $d/$m";
            }

            //Ngày hiện tại
            //if($d == date("j", TIME_NOW) && $m == date("n", TIME_NOW) && date("Y", $time) == date("Y", TIME_NOW))
            if($time == $now_day)
            {
                $day 	= "<div style='background-color:green;color:#fff' title='Ngày hiện tại'><i>$day</i></div>";
            }

            $days[] 	= $day;

            $daysInt[] 	= $time;

            $time 		= $time + 86400;//1day
        }

        $display->add("days",		$days);
        $display->add("daysInt",	$daysInt);

        if($int_from_date >0 && $int_to_date >0 && $int_from_date <= $int_to_date)
        {
            $rooms = Db_Room::getRoomsByHotelId($hotel['id']);

            if($rooms)
            {
                $display->add("rooms",$rooms);

                $room_status = Db_RoomStatus::getRoomsStatusByHotelId($hotel['id'],$int_from_date, $int_to_date);

                $room_prices = Db_RoomPrice::getRoomPricesByHotelId($hotel['id'],$int_from_date, $int_to_date);

                foreach ($rooms as $room)
                {
                    foreach ($daysInt as $time)
                    {
                        if(!isset($room_status[$room['id']]))
                        {
                            $room_status[$room['id']] = array();
                        }

                        if(!isset($room_status[$room['id']][$time]))
                        {
                            $room_status[$room['id']][$time] = array(
                                'total_room' 	=> (int)$room["total_room"],
                                'available'  	=> (int)$room["total_room"],
                                'is_lock'  		=> 0,
                                'day'  			=> $time,
                                'date_over'  	=> ($now_day > $time)
                            );
                        }

                        if(!isset($room_prices[$room['id']]))
                        {
                            $room_prices[$room['id']] = array();
                        }

                        if(!isset($room_prices[$room['id']][$time]))
                        {
                            $room_prices[$room['id']][$time] = array(
                                'price' 	=> (int)$room["price"],
                            );
                        }
                    }
                }

                $display->add("room_status",$room_status);

                $display->add("room_prices",$room_prices);
            }
            else
            {
                $this->setFormError("","Hiện không tìm thấy phòng nào trong khách sạn!");
                $display->add("msg", $this->showFormErrorMessages(1));
            }
        }
        else
        {
            $this->setFormError("","Bạn chọn ngày tháng chưa đúng!");
            $display->add("msg", $this->showFormErrorMessages(1));
        }

        $display->add("hotel",$hotel);

        $this->beginForm(false,'post',false,Url::build_current());
        $display->output('CheckStatusForm');
        $this->endForm();
    }

    function on_submit()
    {
    }
}